package oa.hleast.xswl.controller.admin.banner;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.redis.component.template.RedisRepository;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.pojo.domain.article.CmsArticle;
import oa.hleast.xswl.pojo.form.banner.CmsWebsiteBannerForm;
import oa.hleast.xswl.pojo.vo.web.CmsWebsiteBannerVo;
import oa.hleast.xswl.service.banner.IWebsiteBannerService;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;

/**
 * @author 302
 * @description
 * @date 2021/3/10
 */
@Api(tags = "【系统管理】轮播图管理")
@RestController("WebsiteBannerController")
@RequestMapping("/api.admin/v1/websiteBanner")
@AllArgsConstructor
@Slf4j
public class WebsiteBannerController {

    private final IWebsiteBannerService websiteBannerService;
    private final RedisRepository redisRepository;

    @ApiOperation(value = "新增轮播图", httpMethod = "POST")
    @ApiImplicitParam(name = "article", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsArticle",dataTypeClass = CmsArticle.class)
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody CmsWebsiteBannerForm websiteBannerForm) {
        boolean status = websiteBannerService.add(websiteBannerForm);
        return Result.judge(status);
    }

    @ApiOperation(value = "修改轮播图", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "article", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsArticle",dataTypeClass = CmsArticle.class)
    })
    @PutMapping(value = "update/{id}")
    public Result<Boolean> update(
            @PathVariable @NonNull Integer id,
            @RequestBody @NonNull CmsWebsiteBannerForm websiteBannerForm) {
        boolean status = websiteBannerService.update(websiteBannerForm);
        return Result.judge(status);
    }

    @ApiOperation(value = "轮播图列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式 page分页查询", required = true, paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "title", value = "banner标题", paramType = "query", dataType = "String",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "状态（0全部, 1展示中,10未生效,11已过期）", paramType = "query", dataType = "Boolean",dataTypeClass = Boolean.class)
    })
    @GetMapping("query-list")
    public Result<List<CmsWebsiteBannerVo>> list(
            @RequestParam @NotBlank String queryMode,
            @RequestParam @NonNull Integer page,
            @RequestParam @NonNull Integer limit,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer status) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        Page<CmsWebsiteBannerVo> result = null;
        switch (queryModeEnum) {
            case PAGE:
                result = websiteBannerService.pageList(page, limit, title, status, null);
                return Result.success(result.getRecords(), result.getTotal());
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

    @ApiOperation(value = "轮播图详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "资讯id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("get-details/{id}")
    public Result<CmsWebsiteBannerVo> detail(@PathVariable Integer id) {
        CmsWebsiteBannerVo advert = websiteBannerService.detail(id);
        return Result.success(advert);
    }

    @ApiOperation(value = "删除轮播图", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("delete/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") String ids) {
        //TODO 这个地方要看一下 lArrays.asList 的转换情况 中文逗号也可以转换
        boolean status = websiteBannerService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }

    @ApiOperation(value = "轮播图提前失效", httpMethod = "PUT")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @PutMapping("invalid/{id}")
    public Result<Boolean> invalid(@PathVariable("id") String id) {
        boolean status = websiteBannerService.invalid(id);
        return Result.judge(status);
    }

}
